package com.czkt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czkt.pojo.SalChance;
import com.czkt.pojo.SalPlan;
import com.czkt.pojo.vo.SalPlanVo;
import com.czkt.pojo.vo.SysUserVo;
import com.czkt.service.ISalChanceService;
import com.czkt.service.ISalPlanService;
import com.czkt.utils.PageUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/plan")
public class SalPlanController {

    @Autowired
    ISalPlanService salPlanService;

    @RequestMapping("/list")
    public Object list(SalChance salChance, @RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "2") Integer pageSize, Model model){
        Page<SalPlanVo> page = new Page<>(pageIndex,pageSize);

        SalPlanVo salPlanVo = new SalPlanVo();
        salPlanVo.setSalChance(salChance);

        //获取当前用户信息
        SysUserVo sysUserVo = (SysUserVo) SecurityUtils.getSubject().getPrincipal();

        salChance.setChcDueId(sysUserVo.getUsrId());

        IPage<SalPlanVo> page1 = salPlanService.getPlanVoPage(page,salPlanVo);

        PageUtils pageUtils = new PageUtils();

        BeanUtils.copyProperties(page1,pageUtils);

        model.addAttribute("planPage",pageUtils);

        return "plan/list";
    }

    @Autowired
    ISalChanceService salChanceService;

    @RequestMapping("/add")
    public Object toAdd(Model model){
        SysUserVo sysUserVo = (SysUserVo) SecurityUtils.getSubject().getPrincipal();

        QueryWrapper<SalChance> queryWrapper = new QueryWrapper<>();

        queryWrapper.lambda().eq(sysUserVo.getRole().getRoleId() == 2,SalChance::getChcDueId,sysUserVo.getRole().getRoleId());

        model.addAttribute("users",salChanceService.list(queryWrapper));

        return "plan/add";
    }

    @RequestMapping("/save")
    @Transactional
    public Object save(SalPlan salPlan){
        if (salPlanService.saveOrUpdate(salPlan)){
            return "redirect:/plan/list";
        } else if (salPlan.getPlaId() == null) {
            return "redirect:/plan/add";
        } else {
            return "redirect:/plan/edit?usrId=" + salPlan.getPlaId();
        }
    }

    @RequestMapping("/excute")
    public Object excute(long plaId,Model model){
        QueryWrapper<SalChance> queryWrapper = new QueryWrapper<>();

        SysUserVo sysUserVo = (SysUserVo) SecurityUtils.getSubject().getPrincipal();

        queryWrapper.lambda().eq(sysUserVo.getRole().getRoleId() == 2,SalChance::getChcDueId,sysUserVo.getUsrId());

        model.addAttribute("users",salChanceService.list(queryWrapper));
        model.addAttribute("salPlan",salPlanService.getPlanVoById(plaId));

        return "plan/excute";
    }
}
